
package com.kaluli.project.utils.nanshaxml;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.XMLWriter;

import com.kaluli.project.utils.DateUtil;

/** 
 * 使用dom4j生成xml文档 
 * @author Administrator 
 * 
 */  
public class Dom4jBuildXmlDemo {  
    public void build01(){  
        try {  
            //DocumentHelper提供了创建Document对象的方法  
            Document document = DocumentHelper.createDocument();  
            Namespace namespace = new Namespace("ceb","http://www.chinaport.gov.cn/ceb");
            //添加节点信息  
            Element rootElement = document.addElement(new QName("CEB311Message",namespace));  
            rootElement.addAttribute("version", "1.0");
            rootElement.addAttribute("guid", "556E1CFD-EDED-46B1-946C-B8022E42FC94");
            rootElement.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            //这里可以继续添加子节点，也可以指定内容  
            Element order = rootElement.addElement(new QName("Order",namespace));  
            
            Element orderHead = order.addElement(new QName("OrderHead", namespace));  
          
            
            orderHead.addElement(new QName("guid",namespace)).addText("4CDE1CFD-EDED-46B1-946C-B8022E42FC94");
            orderHead.addElement(new QName("appType",namespace)).addText("1");
            orderHead.addElement(new QName("appTime",namespace)).addText(DateUtil.date2Str(new Date(), "YYYYMMDDhhmmss"));
            orderHead.addElement(new QName("appStatus",namespace)).addText("2");
            orderHead.addElement(new QName("appType",namespace)).addText("1");
            orderHead.addElement(new QName("orderNo",namespace)).addText("");
            orderHead.addElement(new QName("ebpCode",namespace)).addText("1");
            orderHead.addElement(new QName("ebpName",namespace)).addText("1");
            orderHead.addElement(new QName("ebcCode",namespace)).addText("1");
            orderHead.addElement(new QName("ebcName",namespace)).addText("1");
            orderHead.addElement(new QName("freight",namespace)).addText("1");
            orderHead.addElement(new QName("discount",namespace)).addText("1");
            orderHead.addElement(new QName("taxTotal",namespace)).addText("1");
            orderHead.addElement(new QName("acturalPaid",namespace)).addText("1");
            orderHead.addElement(new QName("currency",namespace)).addText("1");
            orderHead.addElement(new QName("buyerRegNo",namespace)).addText("1");
            orderHead.addElement(new QName("buyerName",namespace)).addText("1");
            orderHead.addElement(new QName("buyerIdType",namespace)).addText("1");
            orderHead.addElement(new QName("buyerIdNumber",namespace)).addText("1");
            orderHead.addElement(new QName("payCode",namespace)).addText("1");
            orderHead.addElement(new QName("payName",namespace)).addText("1");
            orderHead.addElement(new QName("payTransactionId",namespace)).addText("1");
            orderHead.addElement(new QName("batchNumbers",namespace)).addText("1");
            orderHead.addElement(new QName("consignee",namespace)).addText("1");
            orderHead.addElement(new QName("consigneeTelephone",namespace)).addText("1");
            orderHead.addElement(new QName("consigneeAddress",namespace)).addText("1");
            orderHead.addElement(new QName("consigneeDistrict",namespace)).addText("1");
            orderHead.addElement(new QName("note",namespace)).addText("1");
            
            
            
            
            
            Element orderList = order.addElement(new QName("OrderList", namespace));  
            orderList.addElement(new QName("itemNo",namespace)).addText("4CDE1CFD");
            orderList.addElement(new QName("gnum",namespace)).addText("1");
            
            
            Element descriptionElement = order.addElement("description");  
            descriptionElement.setText("描述");  
            descriptionElement.addAttribute("language", "sql server");  
            System.out.println(document.asXML()); //将document文档对象直接转换成字符串输出  
            Writer fileWriter = new FileWriter("c:\\module.xml");  
            //dom4j提供了专门写入文件的对象XMLWriter  
            XMLWriter xmlWriter = new XMLWriter(fileWriter);  
            xmlWriter.write(document);  
            xmlWriter.flush();  
            xmlWriter.close();  
            System.out.println("xml文档添加成功！");  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static void main(String[] args) {  
        Dom4jBuildXmlDemo demo = new Dom4jBuildXmlDemo();  
        demo.build01();  
    }  
}  